package adventure.placeable;

import adventure.placeable.trigger.Trigger;
import adventure.placeable.trigger.TriggerEdit;
import adventure.placeable.trigger.TriggerImp;
import adventure.placeable.trigger.results.TriggerResult;
import java.util.ArrayList;

/**
 *
 * @author Michael Hanns
 *
 */
public class PlaceableManagerImp implements PlaceableManager, PlaceableManagerEdit {

    private ArrayList<Item> items;
    private ArrayList<Mobile> mobiles;
    private ArrayList<Trigger> triggers;
    private int totalItemsCreated;
    private int totalMobilesCreated;
    private int totalTriggersCreated;

    public PlaceableManagerImp() {
        items = new ArrayList<Item>();
        mobiles = new ArrayList<Mobile>();
        triggers = new ArrayList<Trigger>();
        totalItemsCreated = 0;
        totalMobilesCreated = 0;
        totalTriggersCreated = 0;
    }

    @Override
    public void newItem() {
        items.add(new ItemImp(++totalItemsCreated));
    }

    @Override
    public void newItem(ItemEdit i) {
        items.add((Item) i);
        totalItemsCreated++;
    }

    @Override
    public void newMobile() {
        mobiles.add(new MobileImp(++totalMobilesCreated));
    }

    @Override
    public void newMobile(MobileEdit m) {
        mobiles.add((Mobile) m);
        totalMobilesCreated++;
    }

    @Override
    public void newTrigger() {
        triggers.add(new TriggerImp(++totalTriggersCreated));
    }

    @Override
    public void newTrigger(TriggerEdit t) {
        triggers.add((Trigger) t);
        totalTriggersCreated++;
    }

    @Override
    public boolean editItem(int id, String field, String val) {
        for (int x = 0; x < items.size(); x++) {
            if (items.get(x).getID() == id) {
                if (field.equalsIgnoreCase("NAME")) {
                    ((ItemEdit) items.get(x)).setName(val);
                } else if (field.equalsIgnoreCase("DESCRIPTION")) {
                    ((ItemEdit) items.get(x)).setDescription(val);
                } else if (field.equalsIgnoreCase("TYPE")) {
                    val = val;
                    ((ItemEdit) items.get(x)).setItemType(val);
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean editItem(int id, String field, int val) {
        for (int x = 0; x < items.size(); x++) {
            if (items.get(x).getID() == id) {
                if (field.equalsIgnoreCase("MODIFIER")) {
                    ((ItemEdit) items.get(x)).setModifier(val);
                } else if (field.equalsIgnoreCase("TOTALUSES")) {
                    ((ItemEdit) items.get(x)).setTotalUses(val);
                } else if (field.equalsIgnoreCase("TICKSREMAIN")) {
                    ((ItemEdit) items.get(x)).setTicksRemaining(val);
                } else if (field.equalsIgnoreCase("CURRENCYVALUE")) {
                    ((ItemEdit) items.get(x)).setCurrencyValue(val);
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean editItem(int id, String field, boolean val) {
        for (int x = 0; x < items.size(); x++) {
            if (items.get(x).getID() == id) {
                if (field.equalsIgnoreCase("CONSUMABLE")) {
                    ((ItemEdit) items.get(x)).setConsumable(val);
                } else if (field.equalsIgnoreCase("TEMPORARY")) {
                    ((ItemEdit) items.get(x)).setTemporary(val);
                } else if (field.equalsIgnoreCase("VISIBLE")) {
                    ((ItemEdit) items.get(x)).setVisible(val);
                } else if (field.equalsIgnoreCase("LIGHTSOURCE")) {
                    ((ItemEdit) items.get(x)).setLightSource(val);
                } else if (field.equalsIgnoreCase("BOUND")) {
                    ((ItemEdit) items.get(x)).setBound(val);
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addItemUnlocksProperty(int id, int x, int y, int z, char dir) {
        dir = Character.toUpperCase(dir);
        for (int c = 0; c < items.size(); c++) {
            if (items.get(c).getID() == id) {
                ((ItemEdit) items.get(c)).addUnlocks(new ItemUnlocksImp(x, y, z, dir));
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean addItemUnlocksProperty(int id, ItemUnlocks i) {
        for (int c = 0; c < items.size(); c++) {
            if (items.get(c).getID() == id) {
                ((ItemEdit) items.get(c)).addUnlocks(i);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean removeItemUnlocksProperties(int id) {
        for (int c = 0; c < items.size(); c++) {
            if (items.get(c).getID() == id) {
                ((ItemEdit) items.get(c)).removeAllUnlocks();
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean editMobile(int id, String field, String val) {
        for (int x = 0; x < mobiles.size(); x++) {
            if (mobiles.get(x).getID() == id) {
                if (field.equalsIgnoreCase("NAME")) {
                    ((MobileEdit) mobiles.get(x)).setName(val);
                } else if (field.equalsIgnoreCase("DESCRIPTION")) {
                    ((MobileEdit) mobiles.get(x)).setDescription(val);
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean editMobile(int id, String field, int val) {
        for (int x = 0; x < mobiles.size(); x++) {
            if (mobiles.get(x).getID() == id) {
                if (field.equalsIgnoreCase("LEVEL")) {
                    ((MobileEdit) mobiles.get(x)).setLevel(val);
                } else if (field.equalsIgnoreCase("MAXHEALTH")) {
                    ((MobileEdit) mobiles.get(x)).setMaxHealth(val);
                } else if (field.equalsIgnoreCase("ATTACK")) {
                    ((MobileEdit) mobiles.get(x)).setAttack(val);
                } else if (field.equalsIgnoreCase("DEFENCE")) {
                    ((MobileEdit) mobiles.get(x)).setDefence(val);
                } else if (field.equalsIgnoreCase("LUCK")) {
                    ((MobileEdit) mobiles.get(x)).setLuck(val);
                } else if (field.equalsIgnoreCase("SPEED")) {
                    ((MobileEdit) mobiles.get(x)).setSpeed(val);
                } else if (field.equalsIgnoreCase("GOLD")) {
                    ((MobileEdit) mobiles.get(x)).setCurrencyAmount(val);
                } else if (field.equalsIgnoreCase("ADDITEM")) {
                    ((MobileEdit) mobiles.get(x)).addInventoryItem(getItem(val));
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }
    
    @Override
    public boolean editMobile(int id, String field, double val) {
        for (int x = 0; x < mobiles.size(); x++) {
            if (mobiles.get(x).getID() == id) {
                if (field.equalsIgnoreCase("VENDORMOD")) {
                    ((MobileEdit) mobiles.get(x)).setVendorModifier(val);
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean editMobile(int id, String field, boolean val) {
        for (int x = 0; x < mobiles.size(); x++) {
            if (mobiles.get(x).getID() == id) {
                if (field.equalsIgnoreCase("AGGRO")) {
                    ((MobileEdit) mobiles.get(x)).setAggro(val);
                } else if (field.equalsIgnoreCase("VISIBLE")) {
                    ((MobileEdit) mobiles.get(x)).setVisible(val);
                } else if (field.equalsIgnoreCase("DEAD")) {
                    ((MobileEdit) mobiles.get(x)).setDead(val);
                } else if (field.equalsIgnoreCase("LIGHTSOURCE")) {
                    ((MobileEdit) mobiles.get(x)).setLightSource(val);
                } else if (field.equalsIgnoreCase("REMOVEITEMS")) {
                    ((MobileEdit) mobiles.get(x)).emptyInventory();
                } else if (field.equalsIgnoreCase("IMMORTAL")) {
                    ((MobileEdit) mobiles.get(x)).setImmortal(val);
                } else if (field.equalsIgnoreCase("VENDOR")) {
                    ((MobileEdit) mobiles.get(x)).setVendor(val);
                } else {
                    return false;
                }
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean editTrigger(Trigger t) {
        for (int x = 0; x < triggers.size(); x++) {
            if (triggers.get(x).getID() == t.getID()) {
                triggers.remove(x);
                triggers.add(t);
                return true;
            }
        }
        return false;
    }

    public boolean editTriggerResult(int trigID, TriggerResult tr, int position) {
        for (int x = 0; x < triggers.size(); x++) {
            if (triggers.get(x).getID() == trigID) {
                return ((TriggerEdit) triggers.get(x)).editResult(tr, position);
            }
        }
        return false;
    }

    @Override
    public boolean deleteItem(int id) {
        for (int x = 0; x < items.size(); x++) {
            if (items.get(x).getID() == id) {
                items.remove(x);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean deleteMobile(int id) {
        for (int x = 0; x < mobiles.size(); x++) {
            if (mobiles.get(x).getID() == id) {
                mobiles.remove(x);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean deleteTrigger(int id) {
        for (int x = 0; x < triggers.size(); x++) {
            if (triggers.get(x).getID() == id) {
                triggers.remove(x);
                return true;
            }
        }
        return false;
    }
    
    @Override
    public int totalItems() {
        return items.size();
    }
    
    @Override
    public int totalMobiles() {
        return mobiles.size();
    }
    
    @Override
    public int totalTriggers() {
        return triggers.size();
    }

    @Override
    public Item getItem(int id) {
        for (int x = 0; x < items.size(); x++) {
            if (items.get(x).getID() == id) {
                return items.get(x).cloneThis();
            }
        }
        return new ItemImp();
    }

    @Override
    public Mobile getMobile(int id) {
        for (int x = 0; x < mobiles.size(); x++) {
            if (mobiles.get(x).getID() == id) {
                return mobiles.get(x).cloneThis();
            }
        }
        return new MobileImp();
    }

    @Override
    public Trigger getTrigger(int id) {
        for (int x = 0; x < triggers.size(); x++) {
            if (triggers.get(x).getID() == id) {
                return triggers.get(x).cloneThis();
            }
        }
        return new TriggerImp();
    }
}
